package tw.com.msig.b2c.car.insurance.web;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import tw.com.msig.b2c.car.common.web.APILibDirector;
import tw.com.msig.b2c.car.common.web.BaseAction;
import tw.com.msig.b2c.car.insurance.service.PolicyService;
import tw.com.msig.b2c.car.insurance.vo.InsItemVo;
import tw.com.msig.b2c.car.insurance.vo.PolicyVo;
import tw.com.msig.b2c.car.setting.service.CarTypeService;
import tw.com.msig.b2c.car.setting.vo.CarTypeVo;

public class ApplicationAction extends BaseAction {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String forwardPage = "step1";

		String sActionType = request.getParameter("sActionType");

		if ("step1".equals(sActionType)) {
			forwardPage = step1(request, response);
		} else if ("step2".equals(sActionType)) {
			forwardPage = step2(request, response);
		} else if ("step5".equals(sActionType)) {
			forwardPage = step5(request, response);
		}
		return mapping.findForward(forwardPage);
	}
	
	public String step1(HttpServletRequest request, HttpServletResponse response) {
		CarTypeService carTypeService = (CarTypeService) APILibDirector
				.getSpringBean("carTypeService");
		List<CarTypeVo> carTypeVos = carTypeService.getAll();
		request.setAttribute("carTypes", carTypeVos);
		return "step1";
	}
	
	public String step2(HttpServletRequest request, HttpServletResponse response) {
		
		String ident = request.getParameter("ident");
		String idNo = request.getParameter("idNo");
		String name = request.getParameter("name");
		String birDate = request.getParameter("birthday");
		String carTypeCode = request.getParameter("carTypeCode");
		String licenseNo = request.getParameter("licenseNo");
		
		PolicyVo policy = new PolicyVo();
		policy.setIdent(ident);
		policy.setIdNo(idNo);
		policy.setName(name);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date d = sdf.parse(birDate);
			Timestamp ts = new Timestamp(d.getTime());
			policy.setBirDate(ts);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		policy.setCarTypeCode(carTypeCode);
		policy.setLicenseNo(licenseNo);
		
		PolicyService policyService = (PolicyService) APILibDirector
				.getSpringBean("policyService");

		
		request.setAttribute("insItems", policyService.getInsItems(policy));
		log.debug("{}", policy);
		request.setAttribute("policyVo", policy);
		
		
		return "step2";
	}
	
	public String step5(HttpServletRequest request, HttpServletResponse response) {
		Timestamp timeStamp = new Timestamp(new Date().getTime());
		String creaTor = StringUtils.trimToEmpty((String)request.getSession().getAttribute("b2cUser"));
		
		Set<InsItemVo> insItemVos = new HashSet<InsItemVo>();
		
		String[] chkInsItemVos = request.getParameterValues("chkInsItemVos");
		for(String chkInsItemVo:chkInsItemVos) {
			InsItemVo vo = new InsItemVo();
			vo.setCode(StringUtils.trimToEmpty(chkInsItemVo));
			vo.setDescription(StringUtils.trimToEmpty(request.getParameter("description_"+chkInsItemVo)));
			vo.setAmounts(StringUtils.trimToEmpty(request.getParameter("amounts_"+chkInsItemVo)));
			vo.setPremium(NumberUtils.toInt(request.getParameter("premium_"+chkInsItemVo)));
			vo.setCreator(creaTor);
			vo.setCreateTime(timeStamp);
			
			insItemVos.add(vo);
		}
		
		PolicyVo vo = new PolicyVo();
		vo.setPolicyNo(String.valueOf(timeStamp.getTime()));
		
		vo.setIdent(StringUtils.trimToEmpty(request.getParameter("ident")));
		vo.setIdNo(StringUtils.trimToEmpty(request.getParameter("idNo")));
		vo.setName(StringUtils.trimToEmpty(request.getParameter("name")));
		try {
			Calendar cl = Calendar.getInstance();
			
			vo.setBegDate(new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(Integer.toString(cl.get(Calendar.YEAR))+"-"+Integer.toString(cl.get(Calendar.MONTH)+1)+"-"+Integer.toString(cl.get(Calendar.DAY_OF_MONTH))).getTime()));
			cl.add(Calendar.YEAR, +1);
			vo.setEndDate(new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(Integer.toString(cl.get(Calendar.YEAR))+"-"+Integer.toString(cl.get(Calendar.MONTH)+1)+"-"+Integer.toString(cl.get(Calendar.DAY_OF_MONTH))).getTime()));
			vo.setBirDate(new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(StringUtils.trimToEmpty(request.getParameter("birDate"))).getTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		vo.setCarTypeCode(StringUtils.trimToEmpty(request.getParameter("carTypeCode")));
		vo.setLicenseNo(StringUtils.trimToEmpty(request.getParameter("licenseNo")));
		vo.setCreator(creaTor);
		vo.setCreateTime(timeStamp);
		
		vo.setInsItemVos(insItemVos);
		PolicyService policyService = (PolicyService) APILibDirector
				.getSpringBean("policyService");
		policyService.insert(vo);
		
		request.setAttribute("policyVo", policyService.getById(vo.getId()));
		CarTypeService carTypeService = (CarTypeService) APILibDirector
				.getSpringBean("carTypeService");
		List<CarTypeVo> carTypeVos = carTypeService.getAll();
		request.setAttribute("carTypes", carTypeVos);
		
		return "step5";
	}
}